
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

public class HeroGame3 extends GameCanvas implements Runnable, CommandListener {

    private LayerManager mLayerManager;
    private Hero mHero;
    private Hero mPrincess;
    int x = 20;
    int y = 20;
    private Opponent mSnail;
    private Opponent mEnemy;
    private Opponent mSnail2;
    private TiledLayer mGrassLayer;
    private TiledLayer mFireLayer;
    private TiledLayer mWaterLayer;
    private int smove = 6;
    private int k = 0;
    private Form menu;
    private Midlet midlet;
    private int sx = 50;
    private int sy = 195;
    private Opponent mOctopus;
    private int upMove = 3;
    private int dowMove = 0;
    private float syEnemy;
    private float sxOctopus;
    private int j;
    private float x1 = 10f;
    private float x2 = 1f;
    private int sx2 = 200;
    private int sy2 = 250;
    private int u;
    public AudioService audio;
    int score = 10000;

    HeroGame3(Midlet midlet, Form menu) {

        super(true);
        mLayerManager = new LayerManager();
        this.menu = menu;
        this.midlet = midlet;

        createGame();
        new Thread(this).start();
        audio = new AudioService("nhac_nen_herogame4.wav");
        audio.playFromResource();
        audio.Start();
    }

    private void createGrass(Graphics g, Image im) {
        try {
            Image backgroundIamge = Image.createImage("/background_1.png");
            mGrassLayer = new TiledLayer(8, 10, backgroundIamge, 30, 30);
            int[] map = {
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, 1, 1,
                3, 3, 4, 3, 3, 4, 3, 4};

            for (int i = 0; i < map.length; i++) {
                int column = i % 8;
                int row = (i - column) / 8;
                mGrassLayer.setCell(column, row, map[i]);

            }
            mLayerManager.append(mGrassLayer);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        mLayerManager.paint(g, 0, 0);
    }

    private void createFire(Graphics g, Image im) {
        try {
            Image backgroundImage = Image.createImage("/background_1.png");
            mFireLayer = new TiledLayer(8, 10, backgroundImage, 30, 30);
            int[] map = {
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 33, 33, 0,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 33, 0, 44, 0,
                0, 0, 39, 30, 0, 0, 0, 33,
                0, 0, 0, 0, 0, 33, 0, 0,
                44, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 39, 39, 39, 39, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0,};
            for (int i = 0; i < map.length; i++) {
                int column = i % 8;
                int row = (i - column) / 8;
                mFireLayer.setCell(column, row, map[i]);
            }
            mLayerManager.append(mFireLayer);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        mLayerManager.append(mFireLayer);
    }

    private void createPrincess(Graphics g) {
        try {
            Image im = Image.createImage("/princess.png");
            mPrincess = new Hero(im, 25, 25);
            mPrincess.setPosition(220, 15);
            mLayerManager.append(mPrincess);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void createHero(Graphics g) {
        try {
            Image mImage = Image.createImage("/Hero.png");
            mHero = new Hero(mImage, 23, 41);
            mHero.setPosition(10, 260);
            mHero.setFrame(mHero.framefomove);
            mHero.defineReferencePixel(12, 21);
            mLayerManager.append(mHero);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    private void createSnail(Graphics g) {
        try {
            Image mImage = Image.createImage("/Fother-snail.png");
            mSnail = new Opponent(mImage, 50, 50);
            mSnail.startformove = 0;
            mSnail.frameformove = 8;
            mSnail.go = "left";
            mSnail.setPosition(sx, sy);
            mSnail.defineReferencePixel(15, 15);
            mLayerManager.append(mSnail);
        } catch (Exception e) {
        }
    }

    private void createEnemy(Graphics g) {
        try {
            Image im = Image.createImage("/enemy.png");
            mEnemy = new Opponent(im, 25, 25);
            mEnemy.go = "up";
            mEnemy.setPosition(150, 30);
            mEnemy.defineReferencePixel(13, 13);
            mLayerManager.append(mEnemy);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void createOctopus(Graphics g) {
        try {
            Image im = Image.createImage("/octopus.png");
            mOctopus = new Opponent(im, 99, 99);
            mOctopus.go = "right";
            mOctopus.setPosition(250, 200);
            mOctopus.defineReferencePixel(50, 55);
            mLayerManager.append(mOctopus);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void createSnail2(Graphics g) {
        try {
            Image mImage = Image.createImage("/Fother-snail.png");
            mSnail2 = new Opponent(mImage, 50, 50);
            mSnail2.startformove = 8;
            mSnail2.frameformove = 0;
            mSnail2.go = "right";
            mSnail2.setPosition(sx, sy);
            mSnail2.defineReferencePixel(15, 15);
            mLayerManager.append(mSnail2);
        } catch (Exception e) {
        }
    }

    private void createGame() {
        try {
            Graphics g = getGraphics();
            Image im = Image.createImage("/tile.png");
            createSnail(g);
            createEnemy(g);
            createOctopus(g);
            createSnail2(g);
            createHero(g);
            createPrincess(g);
            createFire(g, im);
            createGrass(g, im);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        while (true) {
            try {
                Graphics g = getGraphics();
                int ox = mHero.getX();
                int oy = mHero.getY();

                if (mHero.isHeroAlive) {
                    mHero.setPosition(ox, oy);
                    int key = getKeyStates();
                    if ((key & LEFT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.setTransform(Sprite.TRANS_NONE);
                        mHero.setPosition(mHero.getX() - 1, mHero.getY());
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    } else if ((key & RIGHT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.setPosition(mHero.getX() + 1, mHero.getY());
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    }

                    if ((key & UP_PRESSED) != 0) {
                        mHero.setFrame(0 + upMove++);
                        mHero.setPosition(mHero.getX(), mHero.getY() - 1);
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (upMove == 5) {
                            upMove = 3;
                        }
                    }


                    if ((key & DOWN_PRESSED) != 0) {
                        mHero.setFrame(0 + dowMove++);
                        mHero.setPosition(mHero.getX(), mHero.getY() + 1);
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (dowMove == 2) {
                            dowMove = 0;
                        }
                    }

                    if (mHero.collidesWith(mFireLayer, true)) {
                        mHero.setPosition(ox, oy);
                    }

                    if (mHero.getX() < 0) {
                        mHero.setPosition(0, mHero.getY());
                    }
                    if (mHero.getX() > 215) {
                        mHero.setPosition(210, mHero.getY());
                    }
                    if (mHero.getY() > 265) {
                        mHero.setPosition(mHero.getX(), 265);
                    }

                    if (mHero.getY() < 0) {
                        mHero.setPosition(mHero.getX(), 0);
                    }
                    if (mHero.getX() > 210 && mHero.getY() > 250) {
                        mHero.isTranmitted = false;
                    }

                } else {
                    midlet.createGameOver();
                    break;
                }
                if (mHero.isHeroAlive == false) {
                    System.out.println("false");
                    midlet.createTranmitted();
                    break;
                }


                if (mSnail.go == "right") {
                    sx = sx + 1;
                    mSnail.startformove = 10;
                } else {
                    mSnail.startformove = 30;
                    sx = sx - 1;
                }
                if (sx > 120) {
                    mSnail.go = "left";
                }
                if (sx < 50) {
                    mSnail.go = "right";
                }

                mSnail.setPosition(sx, sy);
                mSnail.setFrame(mSnail.startformove + j++);

                if (j > mSnail.frameformove) {
                    j = 0;
                }
                if (mSnail2.go == "left") {
                    sx2 = sx2 - 1;
                    mSnail2.startformove = 30;
                } else {
                    mSnail2.startformove = 10;
                    sx2 = sx2 + 1;
                }
                if (sx2 < 10) {
                    mSnail2.go = "right";
                }
                if (sx2 > 200) {
                    mSnail2.go = "left";
                }

                mSnail2.setPosition(sx2, sy2);
                mSnail2.setFrame(mSnail2.startformove - u--);

                if (u < mSnail2.frameformove) {
                    u = 0;
                }
                if (mOctopus.go == "right") {
                    sxOctopus = sxOctopus + 0.1f;
                    mOctopus.startformove = 0;
                } else {
                    mOctopus.startformove = 3;
                    sxOctopus = sxOctopus - 0.1f;
                }
                if (sxOctopus > 80) {
                    mOctopus.go = "left";
                }
                if (sxOctopus < 10) {
                    mOctopus.go = "right";
                }

                mOctopus.setPosition((int) sxOctopus, 0);
                mOctopus.setFrame(mOctopus.startformove + k++);

                if (k > mOctopus.frameformove) {
                    k = 0;
                }


                if (!"up".equals(mEnemy.go)) {
                    syEnemy = syEnemy - 0.7f;
                    mEnemy.setPosition(mEnemy.getX(), (int) syEnemy);
                } else {
                    syEnemy = syEnemy + 0.7f;
                    mEnemy.setPosition(mEnemy.getX(), (int) syEnemy);
                }
                if (mEnemy.getY() > 165) {
                    mEnemy.go = "down";
                }
                if (mEnemy.getY() < 30) {
                    mEnemy.go = "up";
                }
                if (smove == 1) {
                    smove = 0;
                } else {
                    smove = 1;
                }
                if (mHero.collidesWith(mOctopus, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }

                if (mHero.collidesWith(mSnail, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }

                if (mHero.collidesWith(mEnemy, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }
                if (mHero.collidesWith(mSnail2, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }
                g.setColor(255, 255, 255);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
                g.drawString(String.valueOf(score), 20, 30, g.TOP | g.LEFT);
                score--;
                if (mHero.collidesWith(mPrincess, true)) {
                    midlet.createMenuEndGame();
                    break;
                }

                mLayerManager.paint(g, 0, 0);

                flushGraphics();
                Thread.sleep(20);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void commandAction(Command c, Displayable d) {
    }
}
